package cc.rengu.igas.bsps.common.dao.impl;

import cc.rengu.igas.bsps.common.dao.UserRoleInfoMapper;
import cc.rengu.igas.bsps.common.entity.UserRoleInfo;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户角色表操作接口实现类
 * Created by 王成 on 2018/6/14.
 */
public class UserRoleInfoMapperImpl implements UserRoleInfoMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);

    @Override
    public int insertBspUserRoleInfo(UserRoleInfo userRoleInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertBspUserRoleInfo");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, userRoleInfo, null);
        if (0 != iReturnCode) {
            rglog.error("登记用户角色<{}-{}>信息失败,iReturnCode:<{}>", userRoleInfo.getRoleType(), userRoleInfo.getRoleName(), iReturnCode);
            return iReturnCode;
        }
        return iReturnCode;
    }

    @Override
    public int updateBspUserRoleInfoByPrimaryKey(UserRoleInfo userRoleInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateBspUserRoleInfoByPrimaryKey");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, userRoleInfo, null);
        if (0 != iReturnCode) {
//            rglog.error("更新用户角色信息失败，角色标识<{}>,iReturnCode:<{}>",userRoleInfo.getRoleId(),iReturnCode);
            return iReturnCode;
        }
        return iReturnCode;
    }

    @Override
    public UserRoleInfo selectBspUserRoleInfoByPrimaryKey(String instId, String roleId) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBspUserRoleInfoByPrimaryKey");
        UserRoleInfo userRoleInfo = new UserRoleInfo();
        userRoleInfo.setInstId(instId);
//        userRoleInfo.setRoleId(roleId);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, userRoleInfo, userRoleInfo);
        if (0 != iReturnCode) {
            rglog.error("根据角色ID<{}>获取用户角色信息失败,iReturnCode:<{}>", roleId, iReturnCode);
            return null;
        }
        return userRoleInfo;
    }

    @Override
    public List<UserRoleInfo> selectBspUserRoleInfoByRoleType(String instId, String roleType) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBspUserRoleInfoByRoleType");
        UserRoleInfo userRoleInfo = new UserRoleInfo();
        userRoleInfo.setInstId(instId);
        userRoleInfo.setRoleType(roleType);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, userRoleInfo, UserRoleInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据角色类型<{}>获取用户角色信息失败,iReturnCode:<{}>", roleType);
            return null;
        }
        return objectList.stream().map(item -> (UserRoleInfo) item).collect(Collectors.toList());
    }

    @Override
    public List<UserRoleInfo> selectBspUserRoleInfoByInstId(String instId) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBspUserRoleInfoByInstId");
        UserRoleInfo userRoleInfo = new UserRoleInfo();
        userRoleInfo.setInstId(instId);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, userRoleInfo, UserRoleInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据机构标识<{}>获取用户角色信息失败,iReturnCode:<{}>", instId);
            return null;
        }
        return objectList.stream().map(item -> (UserRoleInfo) item).collect(Collectors.toList());
    }
}
